package com.um.jdy.common.commons.utils;

import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import com.um.jdy.common.commons.properties.Kuaidi100Properties;
import com.um.springboot.starter.code.CommonCode;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.JSONUtil;
import com.um.springboot.starter.utils.PayUtil;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2021/6/17 4:19 下午
 */
@Slf4j
@Component
public class Kuaidi100Util {

    public static String KEY;
    public static String CUSTOMER;

    @Autowired
    Kuaidi100Properties kuaidi100Properties;

    @PostConstruct
    public void init() {
        Kuaidi100Util.KEY = kuaidi100Properties.getKey();
        Kuaidi100Util.CUSTOMER = kuaidi100Properties.getCustomer();
    }

    /**
     * 查询物流轨迹
     */
    public static Object queryTrack(String expressCompany, String no, String phone){

        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(expressCompany);
        queryTrackParam.setNum(no);
        queryTrackParam.setPhone(phone);
        String param = new Gson().toJson(queryTrackParam);

        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(CUSTOMER);
        queryTrackReq.setSign(SignUtils.querySign(param ,KEY,CUSTOMER));

        IBaseClient baseClient = new QueryTrack();
        HttpResult result;
        try {
            result = baseClient.execute(queryTrackReq);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(CommonCode.Failed);
        }
        if(null == result){
            throw new ServiceException(CommonCode.Failed);
        }
        return formatResponse(result);
    }

    public static Object formatResponse(HttpResult result){
        log.info("快递100查询结果："+result.toString());
        if(!PayUtil.isProd()){
            String body = "{\"message\":\"ok\",\"nu\":\"JT0004301991791\",\"ischeck\":\"0\",\"com\":\"jtexpress\",\"status\":\"200\",\"data\":[{\"time\":\"2021-12-15 17:19:28\",\"context\":\"【杭州市】您的包裹已存放至【驿站】，记得早点来取它回家！\",\"ftime\":\"2021-12-15 17:19:28\",\"areaCode\":\"CN330102000000\",\"areaName\":\"浙江,杭州市,上城区\",\"status\":\"投柜或驿站\",\"location\":\"浙江省 杭州市 上城区\",\"areaCenter\":\"120.184349,30.25446\",\"areaPinYin\":\"shang cheng qu\",\"statusCode\":\"501\"},{\"time\":\"2021-12-15 14:17:31\",\"context\":\"【杭州市】【杭州网点】的极兔小哥正在派件\",\"ftime\":\"2021-12-15 14:17:31\",\"areaCode\":\"CN330102000000\",\"areaName\":\"浙江,杭州市,上城区\",\"status\":\"派件\",\"location\":\"浙江省 杭州市 上城区\",\"areaCenter\":\"120.184349,30.25446\",\"areaPinYin\":\"shang cheng qu\",\"statusCode\":\"5\"},{\"time\":\"2021-12-15 13:58:18\",\"context\":\"【杭州市】 快件到达【杭州网点】\",\"ftime\":\"2021-12-15 13:58:18\",\"areaCode\":\"CN330102000000\",\"areaName\":\"浙江,杭州市,上城区\",\"status\":\"在途\",\"location\":\"浙江省 杭州市 上城区\",\"areaCenter\":\"120.184349,30.25446\",\"areaPinYin\":\"shang cheng qu\",\"statusCode\":\"0\"},{\"time\":\"2021-12-15 04:11:20\",\"context\":\"【杭州市】快件离开【杭州转运中心】已发往【杭州江干四季青网点】\",\"ftime\":\"2021-12-15 04:11:20\",\"areaCode\":\"CN330109000000\",\"areaName\":\"浙江,杭州市,萧山区\",\"status\":\"干线\",\"location\":\"浙江省 杭州市 萧山区\",\"areaCenter\":\"120.493286,30.28333\",\"areaPinYin\":\"xiao shan qu\",\"statusCode\":\"1002\"},{\"time\":\"2021-12-15 02:09:52\",\"context\":\"【杭州市】 快件到达【杭州转运中心】\",\"ftime\":\"2021-12-15 02:09:52\",\"areaCode\":\"CN330109000000\",\"areaName\":\"浙江,杭州市,萧山区\",\"status\":\"干线\",\"location\":\"浙江省 杭州市 萧山区\",\"areaCenter\":\"120.493286,30.28333\",\"areaPinYin\":\"xiao shan qu\",\"statusCode\":\"1002\"},{\"time\":\"2021-12-14 21:08:34\",\"context\":\"【上海市】快件离开【上海浦西转运中心】已发往【杭州转运中心】\",\"ftime\":\"2021-12-14 21:08:34\",\"areaCode\":\"CN310118000000\",\"areaName\":\"上海,上海,青浦区\",\"status\":\"干线\",\"location\":\"上海 上海市 青浦区\",\"areaCenter\":\"121.124178,31.150681\",\"areaPinYin\":\"qing pu qu\",\"statusCode\":\"1002\"},{\"time\":\"2021-12-14 20:54:22\",\"context\":\"【上海市】 快件到达【上海浦西转运中心】\",\"ftime\":\"2021-12-14 20:54:22\",\"areaCode\":\"CN310118000000\",\"areaName\":\"上海,上海,青浦区\",\"status\":\"干线\",\"location\":\"上海 上海市 青浦区\",\"areaCenter\":\"121.124178,31.150681\",\"areaPinYin\":\"qing pu qu\",\"statusCode\":\"1002\"},{\"time\":\"2021-12-14 17:25:58\",\"context\":\"【上海市】快件离开【上海杨浦黄兴路网点】已发往【上海浦西转运中心】\",\"ftime\":\"2021-12-14 17:25:58\",\"areaCode\":\"CN310110000000\",\"areaName\":\"上海,上海,杨浦区\",\"status\":\"干线\",\"location\":\"上海 上海市 杨浦区\",\"areaCenter\":\"121.526077,31.259541\",\"areaPinYin\":\"yang pu qu\",\"statusCode\":\"1002\"},{\"time\":\"2021-12-14 09:03:58\",\"context\":\"【上海市】【上海杨浦黄兴路网点】已取件。\",\"ftime\":\"2021-12-14 09:03:58\",\"areaCode\":\"CN310110000000\",\"areaName\":\"上海,上海,杨浦区\",\"status\":\"揽收\",\"location\":\"上海 上海市 杨浦区\",\"areaCenter\":\"121.526077,31.259541\",\"areaPinYin\":\"yang pu qu\",\"statusCode\":\"1\"}],\"state\":\"5\",\"condition\":\"00\",\"routeInfo\":{\"from\":{\"number\":\"CN310110000000\",\"name\":\"上海,上海,杨浦区\"},\"cur\":{\"number\":\"CN330102000000\",\"name\":\"浙江,杭州市,上城区\"},\"to\":null},\"isLoop\":false}\n";
            return JSONUtil.parseJSONToMap(body);
        }
        if(200 != result.getStatus()){
            throw new ServiceException(CommonCode.Failed);
        }
        Map data = JSONUtil.parseJSONToMap(result.getBody());
        if(null != data.get("returnCode")){
            throw new ServiceException(StringUtil.getTrim(data.get("message")));
        }
        return data;
    }
}

